using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
//using UnityEngine.UIElements;


public class DoorToNextLevel : MonoBehaviour
{
    public Text CoinAllNumber;

    public GameObject loadingScreen;
    public Slider slider;
    public Text progressText;
    //public Button Level_1;
    //public Button[] LevelSelect;
    //public GameObject allLevelSelect;

    // Start is called before the first frame update
    //private void Awake()
    //{
    //    Date.Intance.LevelSelect = new Button[allLevelSelect.transform.childCount];
    //    for (int i = 0; i < LevelSelect.Length; i++)
    //    {
    //        Date.Intance.LevelSelect[i] = allLevelSelect.GetComponentInChildren<Button>();

    //    }
    //}

    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    public void LoadLevel(int sceneIndex)
    {
        StartCoroutine(AsyncLoadLevel(sceneIndex));
    }

    IEnumerator AsyncLoadLevel(int sceneIndex)
    {
        AsyncOperation operation = SceneManager.LoadSceneAsync(sceneIndex);
        loadingScreen.SetActive(true);
        while (!operation.isDone)
        {
            float progress = operation.progress / 0.9f;
            slider.value = progress;
            progressText.text = Mathf.FloorToInt(progress * 100f).ToString() + "%";
            yield return null;
        }
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if(other.gameObject .CompareTag ("Player")&&other.GetType().ToString ()=="UnityEngine.CapsuleCollider2D")
        {
            if(int.Parse(CoinAllNumber.text) <= CoinUI .CurrentCoinQuanity /*&& Input.GetKey(KeyCode.E)*/)
            {
                LoadLevel(SceneManager.GetActiveScene().buildIndex + 1);
                //SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);

            }

        }
    }
    
}
